from flask import Flask, render_template, Response, request
from apps.pojo.Entity import db
from apps.service.LoginService import LoginService
from apps.controller.UserController import user_bp
app = Flask(__name__)
#注册蓝图
app.register_blueprint(user_bp)

# #设置数据库连接
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:lizequn23@localhost:3306/flask'

# 此处初始化数据表
with app.app_context():
    db.init_app(app)
    # db.drop_all()
    # db.create_all()


@app.route('/')  # 根路径
def tologin():
    return render_template('login.html')


@app.route('/dologin', methods=['POST'])
def dologin():
    param = request.form
    username = param.get('username')
    password = param.get('password')
    # 调用登录方法
    user = LoginService.login(username, password)
    if user:
        return render_template('index.html', user=user)
    else:
        return render_template('login.html', msg='用户名或密码错误')
#
# if result:
#     return render_template('index.html')
# else:
#     return render_template('login.html')

@app.route('/index_v1')
def index_v1():
    return render_template('index_v1.html')


@app.route('/index_v3')
def index_v3():
    return render_template('index_v3.html')

if __name__ == '__main__':
    # 运行app，默认运行在5000
    # 默认是host='127.0.0.1', port=5000端口
    # app.run()
    app.run(debug=True, port=5000, host='0.0.0.0')

'运行后http://127.0.0.1:5000访问这个地址，即可看到hello，world了'
